package io.terminus.handler;

import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler;
import io.terminus.configuration.Context;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author li.chunlong lcl253-137@alibaba-inc.com
 * @desc 自定义动态表名处理器
 * @date 2021-09-28
 */
@Component
public class DynamicTableNameHandler implements TableNameHandler {

    @Override
    public String dynamicTableName(String sql, String tableName) {
        // 如果返回字符串为空，则动态表名取原先的值，不做替换
        if (Objects.isNull(Context.dynamicTableName.get())) {
            return tableName;
        }
        return Context.dynamicTableName.get();
    }
}
